﻿using pachon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PachonDemo
{
    public partial class AddForm : Form
    {
        Comm com = new Comm();
        pcDAL pd = new pcDAL();

        Encoding _endcode = Encoding.UTF8;

        string _soucestr = null;
        string _webId = null;

        public AddForm()
        {
            InitializeComponent();
            _webId = com.GetFormCode(FormType.pachon);
        }

        private void butwebregex1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtinWebUrl.Text) == true)
            {
                return;
            }
            else if (string.IsNullOrEmpty(txtwebregex1.Text) == true)
            {
                return;
            }
            else
            {
                if (cbendcode.Text.Equals("gb2312"))
                    _endcode = Encoding.Default;
                if (string.IsNullOrEmpty(_soucestr)==true)
                    _soucestr = com.GetUrlContent(txtinWebUrl.Text, _endcode);
                List<string> lsUrl = com.GetRegexString(_soucestr, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
                string strr = null;
                if (lsUrl.Count > 0)
                {
                    foreach (string ss in lsUrl)
                    {
                        strr += ss + "\r\n";
                    }
                }
                ShowMsg sm = new ShowMsg(strr);
                sm.ShowDialog();
            }
        }

        private void butsaveEdit_Click(object sender, EventArgs e)
        {
            
        }

        private void buttitle_regex1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwebregex1.Text) == true)
               return;
            if (string.IsNullOrEmpty(_soucestr) == true)
                _soucestr = com.GetUrlContent(txtinWebUrl.Text, _endcode);

            List<string> lsUrl = com.GetRegexString(_soucestr, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
            string urls = lsUrl.FirstOrDefault();
            if (urls == null)
                return;
            else
            {
                string htmlstrrr = com.GetUrlContent(urls, _endcode);
                List<string> strurlhtl = com.GetRegexString(htmlstrrr, txttitle_regex1.Text, txttitle_regex2.Text, txttitle_regex3.Text);
                ShowMsg sm = new ShowMsg(strurlhtl.FirstOrDefault());
                sm.ShowDialog();
            }
        }

        private void buttime_regex1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwebregex1.Text) == true)
                return;
            if (string.IsNullOrEmpty(_soucestr) == true)
                _soucestr = com.GetUrlContent(txtinWebUrl.Text, _endcode);
            List<string> lsUrl = com.GetRegexString(_soucestr, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
            string urls = lsUrl.FirstOrDefault();
            if (urls == null)
                return;
            else
            {
                string htmlstrrr = com.GetUrlContent(urls, _endcode);
                List<string> strurlhtl = com.GetRegexString(htmlstrrr, txttime_regex1.Text, txttime_regex2.Text, txttime_regex3.Text);
                ShowMsg sm = new ShowMsg(strurlhtl.FirstOrDefault());
                sm.ShowDialog();
            }
        }

        private void butcontent_regex1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwebregex1.Text) == true)
                return;
            if (string.IsNullOrEmpty(_soucestr) == true)
                _soucestr = com.GetUrlContent(txtinWebUrl.Text, _endcode);

            List<string> lsUrl = com.GetRegexString(_soucestr, txtwebregex1.Text, txtwebregex2.Text, txtwebregex3.Text, txtappendurl.Text);
            string urls = lsUrl.FirstOrDefault();
            if (urls == null)
                return;
            else
            {
                string htmlstrrr = com.GetUrlContent(urls, _endcode);
                List<string> strurlhtl = com.GetRegexString(htmlstrrr, txtcontent_regex1.Text, txtcontent_regex2.Text, txtcontent_regex3.Text);
                ShowMsg sm = new ShowMsg(strurlhtl.FirstOrDefault());
                sm.ShowDialog();
            }
        }

        private void butsavetow_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtinWebUrl.Text) == true)
            {
                MessageBox.Show("入口网址不能为空...");
                return;
            }
            else if (string.IsNullOrEmpty(txtwebregex1.Text) == true)
            {
                return;
            }
            else
            {
                PC_INWEB pc_i = new PC_INWEB();
                pc_i.id = _webId;
                pc_i.weburl = txtWebUrl.Text;
                pc_i.inweburl = txtinWebUrl.Text;
                pc_i.webname = txtWebName.Text;
                pc_i.cando = cbcando.Checked == true ? "1" : "0";
                pc_i.endcode = cbendcode.Text;
                pc_i.webregex1 = txtwebregex1.Text;
                pc_i.webregex2 = txtwebregex2.Text;
                pc_i.webregex3 = txtwebregex3.Text;
                pc_i.creatertime = System.DateTime.Now;
                pc_i.uddatetime = System.DateTime.Now;

                pc_i.pccity = txtcity.Text;
                pc_i.pctype = txttype.Text;
                pc_i.appendurl = txtappendurl.Text;

                PC_CONTENT_REGEX pc_c_r = new PC_CONTENT_REGEX();
                pc_c_r.id = com.RandCode(8);
                pc_c_r.weburl_id = _webId;
                pc_c_r.title_regex1 = txttitle_regex1.Text;
                pc_c_r.title_regex2 = txttitle_regex2.Text;
                pc_c_r.title_regex3 = txttitle_regex3.Text;
                pc_c_r.time_regex1 = txttime_regex1.Text;
                pc_c_r.time_regex2 = txttime_regex2.Text;
                pc_c_r.time_regex3 = txttime_regex3.Text;
                pc_c_r.content_regex1 = txtcontent_regex1.Text;
                pc_c_r.content_regex2 = txtcontent_regex2.Text;
                pc_c_r.content_regex3 = txtcontent_regex3.Text;

                int ret_i_pccr = pd.InsertPC_CONTENT_REGEX(pc_c_r);
                int ret_i = pd.InsertPC_INWEB(pc_i);

                if (ret_i == 1 && ret_i_pccr == 1)
                {
                    MessageBox.Show("插入保存成功...");
                }
                else
                    MessageBox.Show("error...");
            }
        }
    }
}
